package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.AbToastUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.model.AttentionModel;
import com.gouaixin.bee.beeframework2.model.HeightModel;
import com.gouaixin.bee.beeframework2.model.HelloModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class TuijianAdapter extends BaseAdapter {


    Context context;
    //	ArrayList<HashMap<String, Object>> al;
    LayoutInflater mInflater;
    JSONArray jsonArray;
    JSONObject jsonOb;

    String jid = "";
    String jicon = "";
    String jname = "";
    String cid = "";
    String cname = "";

    boolean isZhaohu[];//是否已经打过招呼


    public TuijianAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);

        setJson(new JSONArray());
    }

    public void setJson(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }


    public TuijianAdapter(Context context, ArrayList<HashMap<String, Object>> al) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);


    }

    public int getCount() {
        return jsonArray.length();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder;


    public View getView(final int position, View convertView, ViewGroup parent) {

        cid = PreferencesUtils.getString(context, "cid");
        cname = PreferencesUtils.getString(context, "cname");

//        if (convertView == null) {
//        View convertView;
            convertView = mInflater.inflate(R.layout.tuijian_item, null);
            viewHolder = new ViewHolder();

            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.icon01 = (ImageView) convertView.findViewById(R.id.icon01);
            viewHolder.icon02 = (ImageView) convertView.findViewById(R.id.icon02);
            viewHolder.icon03 = (ImageView) convertView.findViewById(R.id.icon03);

            viewHolder.qianming = (TextView) convertView.findViewById(R.id.qianming);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.dianzanNum = (TextView) convertView.findViewById(R.id.dianzanNum);
            viewHolder.age = (TextView) convertView.findViewById(R.id.age);
            viewHolder.height = (TextView) convertView.findViewById(R.id.height);
            viewHolder.address = (TextView) convertView.findViewById(R.id.address);
            viewHolder.juli = (TextView) convertView.findViewById(R.id.juli);
            viewHolder.zhaohu = (ImageView) convertView.findViewById(R.id.zhaohu);
            viewHolder.guanzhu = (ImageView) convertView.findViewById(R.id.guanzhu);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }

        try {
            if (jsonArray.length() > 0) {


                jsonOb = (JSONObject) jsonArray.opt(position);
                if (jsonOb != null) {
                    jid = jsonOb.getString("id");
                    jicon = jsonOb.getString("icon");
                    if (!jicon.equals("")) {
                        mLog.e("加载推荐Adapter：jid=" + jid);
                        mLog.e("加载推荐Adapter：jicon=" + jicon);

                        try {
                            ImageLoader.getInstance().displayImage(jicon, viewHolder.icon, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String s, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String s, View view, FailReason failReason) {
                                    mLog.e("加载推荐Adapter：错误=" + s);

                                    if (s.endsWith(".jpg")) {
                                        mLog.e("将jpg转png");
                                        s = s.replaceAll(".jpg", ".png");
                                    } else if (s.endsWith(".png")) {
                                        mLog.e("将png转jpg");
                                        s = s.replaceAll(".png", ".jpg");
                                    }

                                    mLog.e("加载推荐Adapter：转化后的jicon=" + s);

                                    ImageLoader.getInstance().displayImage(s, viewHolder.icon);

                                }

                                @Override
                                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                    mLog.e("加载推荐Adapter：成功=" + s);

                                }

                                @Override
                                public void onLoadingCancelled(String s, View view) {

                                }
                            });
                        } catch (Exception e) {
                            mLog.e("加载推荐Adapter：捕获错误=" + e.getMessage());
                        }

                    } else {
                        viewHolder.icon.setBackgroundResource(R.drawable.ic_other_avatar_status02);
                    }

                    viewHolder.qianming.setText(jsonOb.getString("qianming"));
                    viewHolder.name.setText(jsonOb.getString("name"));
                    viewHolder.dianzanNum.setText(jsonOb.getString("dianzan"));

//                    viewHolder.height.setText(jsonOb.getString("height"));
//                    viewHolder.juli.setText(jsonOb.getString("juli"));

                    viewHolder.height.setText(HeightModel.getHeight(context, jid));
                    viewHolder.juli.setText(HeightModel.getJuli(context, jid));


                    viewHolder.age.setText(jsonOb.getString("age"));
                    String city = PreferencesUtils.getString(context, "city", "当前城市");
                    String address = PreferencesUtils.getString(context, "address", "当前城市");
                    viewHolder.address.setText(address);

                    JSONArray jsonArray1 = jsonOb.getJSONArray("photo");
                    mLog.e("对象详情：jsonArray1.length()=" + jsonArray1.length());
                    mLog.e("对象详情：photo=" + jsonArray1.toString());
                    if(jsonArray1.length()==2){
                        ImageLoader.getInstance().displayImage(jsonArray1.opt(0).toString(), viewHolder.icon01);
                        ImageLoader.getInstance().displayImage(jsonArray1.opt(1).toString(), viewHolder.icon02);

                        mLog.e("对象详情：隐藏icon3");
                        viewHolder.icon02.setVisibility(View.GONE);

                        if(jsonArray1.opt(1).toString().contains(jid)){
                            viewHolder.icon02.setVisibility(View.VISIBLE);
                        }else {
                            viewHolder.icon02.setVisibility(View.GONE);
                        }
                    }else  if(jsonArray1.length()==1){
                        ImageLoader.getInstance().displayImage(jsonArray1.opt(0).toString(), viewHolder.icon01);

                        mLog.e("对象详情：隐藏icon3");
                        viewHolder.icon02.setVisibility(View.GONE);
                        viewHolder.icon03.setVisibility(View.GONE);

                    }else {

                            ImageLoader.getInstance().displayImage(jsonArray1.opt(0).toString(), viewHolder.icon01);

                        ImageLoader.getInstance().displayImage(jsonArray1.opt(1).toString(), viewHolder.icon02);
                        ImageLoader.getInstance().displayImage(jsonArray1.opt(2).toString(), viewHolder.icon03);

                        mLog.e("对象详情：显示icon3");
                        viewHolder.icon03.setVisibility(View.VISIBLE);

                        if(jsonArray1.opt(1).toString().contains(jid)){
                            viewHolder.icon02.setVisibility(View.VISIBLE);
                        }else {
                            viewHolder.icon02.setVisibility(View.GONE);
                        }

                        if(jsonArray1.opt(2).toString().contains(jid)){
                            viewHolder.icon03.setVisibility(View.VISIBLE);
                        }else {
                            viewHolder.icon03.setVisibility(View.GONE);
                        }
                    }
//                    for (int i = 0; i < 3; i++) {
//                        switch (i) {
//                            case 0:
//                                if (0 < jsonArray1.length()) {
//                                    ImageLoader.getInstance().displayImage(jsonArray1.opt(i).toString(), viewHolder.icon01);
//                                    if (jsonArray1.opt(i).toString().equals("") || jsonArray1.opt(i).toString().equals("null")) {
//                                        viewHolder.icon01.setVisibility(View.GONE);
//                                    }
//                                } else {
//                                    viewHolder.icon01.setVisibility(View.GONE);
//                                }
//
//                                break;
//                            case 1:
//                                if (1 < jsonArray1.length()) {
//                                    ImageLoader.getInstance().displayImage(jsonArray1.opt(i).toString(), viewHolder.icon02);
//                                    if (jsonArray1.opt(i).toString().equals("") || jsonArray1.opt(i).toString().equals("null")) {
//                                        viewHolder.icon02.setVisibility(View.GONE);
//                                    }
//                                } else {
//                                    viewHolder.icon02.setVisibility(View.GONE);
//                                }
//                                break;
//                            case 2:
//                                if (2 < jsonArray1.length()) {
//                                    ImageLoader.getInstance().displayImage(jsonArray1.opt(i).toString(), viewHolder.icon03);
//                                    if (jsonArray1.opt(i).toString().equals("") || jsonArray1.opt(i).toString().equals("null")) {
//                                        viewHolder.icon03.setVisibility(View.GONE);
//                                    }
//                                } else {
//                                    viewHolder.icon03.setVisibility(View.GONE);
//                                }
//
//
//                                break;
//                        }
//                    }
                }
            }
        } catch (JSONException e) {
            mLog.e("json解析异常" + e.getMessage());
            e.printStackTrace();
        }


        final boolean isZhaohu = PreferencesUtils.getBoolean(context, jid + "isZhaohu", false);
        final boolean isGuanzhu = PreferencesUtils.getBoolean(context, jid + "isGuanzhu", false);

        viewHolder.zhaohu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    jsonOb = (JSONObject) jsonArray.opt(position);
                    if (jsonOb != null) {
                        jid = jsonOb.getString("id");
                        jicon = jsonOb.getString("icon");
                        jname = jsonOb.getString("name");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mLog.e("点击了打招呼");
                mLog.e("position=" + position);
                mLog.e("isZhaohu=" + isZhaohu);
                mLog.e("jid=" + jid);
                mLog.e("jicon=" + jicon);
                mLog.e("cid=" + cid);
                mLog.e("jname=" + jname);
                if (isZhaohu == false) {
                    //判断是否可以打招呼
                    boolean hasZhaohu=PreferencesUtils.getBoolean(context,"hasZhaohu",false);
                    mLog.e("点击了打招呼：hasZhaohu="+hasZhaohu);
                    if(hasZhaohu==false){
                        AbToastUtil.showToast(context, "打招呼成功！");
                    }


                    HelloModel.oneZhaohu(context, jid, jicon, cid, jname);


                    notifyDataSetChanged();
                } else {
                    AbToastUtil.showToast(context, "已打招呼！");
                }
            }
        });

        viewHolder.guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    jsonOb = (JSONObject) jsonArray.opt(position);
                    if (jsonOb != null) {
                        jid = jsonOb.getString("id");
                        jicon = jsonOb.getString("icon");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


                mLog.e("点击了关注");
                mLog.e("position=" + position);
                mLog.e("isGuanzhu=" + isGuanzhu);
                mLog.e("jid=" + jid);
                mLog.e("jicon=" + jicon);
                mLog.e("cid=" + cid);
                if (isGuanzhu == false) {
                    AttentionModel.addGuanzhu(context, jid, jicon, cid);

                    AbToastUtil.showToast(context, "添加关注成功！");

                    notifyDataSetChanged();
                } else {
                    AbToastUtil.showToast(context, "已添加关注");
                }
            }
        });

        if (isZhaohu == false) {
            viewHolder.zhaohu.setImageResource(R.drawable.home_hi1);
        } else {
            viewHolder.zhaohu.setImageResource(R.drawable.home_hi2);
        }

        if (isGuanzhu == false) {
            viewHolder.guanzhu.setImageResource(R.drawable.home_follow1);
        } else {
            viewHolder.guanzhu.setImageResource(R.drawable.home_follow2);
        }

        return convertView;
    }

    /**
     * 视图控制类
     */
    class ViewHolder {
        ImageView icon;
        ImageView icon01;
        ImageView icon02;
        ImageView icon03;
        TextView qianming;
        TextView name;
        TextView dianzanNum;
        TextView age;
        TextView height;
        TextView address;
        TextView juli;
        ImageView zhaohu;
        ImageView guanzhu;

    }


}
